package com.zh.javaguide.multithread;
/**
 * 死锁
 */
public class DeadLockDemo {
	private static Object resource1 = new Object();//资源 1
	private static Object resource2 = new Object();//资源 2

	public static void main(String[] args) {
		new Thread(() -> {
			synchronized (resource1) {
				System.out.println(Thread.currentThread() + "get resource1");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread() + "waiting get resource2");
				synchronized (resource2) {
					System.out.println(Thread.currentThread() + "get resource2");
				}
			}
		}, "线程 1").start();

		/**
		 * 产生死锁
		 */
		new Thread(() -> {
			synchronized (resource2) {
				System.out.println(Thread.currentThread() + "get resource2");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread() + "waiting get resource1");
				synchronized (resource1) {
					System.out.println(Thread.currentThread() + "get resource1");
				}
			}
		}, "线程 2").start();

		/**
		 * 改进，避免死锁
		 */
//		new Thread(() -> {
//			synchronized (resource1) {
//				System.out.println(Thread.currentThread() + "get resource1");
//				try {
//					Thread.sleep(1000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				System.out.println(Thread.currentThread() + "waiting get resource2");
//				synchronized (resource2) {
//					System.out.println(Thread.currentThread() + "get resource2");
//				}
//			}
//		}, "线程 3").start();
	}

	/**
	 * 结果
	 *
	 * Thread[线程 1,5,main]get resource1
	 * Thread[线程 2,5,main]get resource2
	 * Thread[线程 1,5,main]waiting get resource2
	 * Thread[线程 2,5,main]waiting get resource1
	 */
}
